<?php


namespace app\common\service\lib;


use Elasticsearch\ClientBuilder;

class Es
{

    protected $client;


    /**
     * Es constructor.
     */
    public function __construct()
    {
        $host = config("setting.elasticsearch.host");
        $port = config("setting.elasticsearch.port");
        $this->client = ClientBuilder::create()->setHosts(["47.101.202.80:9200"])->build();
    }


    /**
     * Author:YangXinPeng
     * 判断索引是否存在
     * @param $indexName
     * @return bool
     */
    public function exist_index($indexName)
    {
        $params = ['index' => $indexName];
        return $this->client->indices()->exists($params);
    }

    /**
     * Author:YangXinPeng
     * 创建索引
     * @param $params
     * @return array
     */
    public function createIndex($params)
    {
        $response = $this->client->indices()->create($params);
        return $response;
    }

    /**
     * Author:YangXinPeng
     * 添加数据
     * @param $params
     * @return array
     */
    public function addIndex($params)
    {
        $response = $this->client->index($params);
        return $response;
    }

    /**
     * Author:YangXinPeng
     *商品搜索
     * @param $params
     * @return array
     */
    public function match($params)
    {
        $results = $this->client->search($params);
        return $results;
    }

    /**
     * Author:YangXinPeng
     * 删除索引
     * @param $params
     * @return array
     */
    public function delIndex($params)
    {
        $response = $this->client->indices()->delete($params);
        return $response;
    }

    /**
     * Author:YangXinPeng
     * 更新索引
     * @param $params
     * @return array
     */
    public function updateIndex($params)
    {
        $response = $this->client->update($params);
        return $response;
    }
}